<?php
// +----------------------------------------------------------------------
// | OneKeyAdmin [ Believe that you can do better ]
// +----------------------------------------------------------------------
// | Copyright (c) 2020-2023 http://onekeyadmin.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: MUKE <513038996@qq.com>
// +----------------------------------------------------------------------
namespace plugins\cms\api\controller;

use think\exception\ValidateException;
use app\api\BaseController;
use plugins\cms\api\validate\News as Validate;
use plugins\cms\api\model\NewsCommentLike;
/**
 * 我的新闻评论点赞
 */
class UserNewsCommentLike extends BaseController
{
    /**
     * 用户权鉴
     */
    public $middleware = [\app\api\middleware\AuthCheck::class];
    
    /**
     * 保存新建的资源
     */
    public function save()
    {
        if ($this->request->isPost()) {
            try {
                $input = input('post.');
                validate(Validate::class)->scene('userCommentLikeSave')->check($input);
            } catch ( ValidateException $e ) {
                return json(['status' => 'error', 'message' => $e->getError()]);
            }
            $exits = NewsCommentLike::where('user_id', $this->request->userInfo->id)->where('comment_id',$input['id'])->find(); 
            if ($exits) {
                return json(['status' => 'success', 'message' => '保存成功']);
            }
            NewsCommentLike::create([
                'user_id'     => $this->request->userInfo->id,
                'comment_id'  => $input['id'],
                'create_time' => date('Y-m-d H:i:s')
            ]);
            return json(['status' => 'success', 'message' => '点赞成功']);
        }
    }
    
    /**
     * 删除指定资源
     */
    public function delete() 
    {
        if ($this->request->isPost()) {
            try {
                $input = input('post.');
                validate(Validate::class)->scene('delete')->check($input);
            } catch ( ValidateException $e ) {
                return json(['status' => 'error', 'message' => $e->getError()]);
            }
            NewsCommentLike::where('comment_id', $input['id'])->where('user_id', $this->request->userInfo->id)->delete();
            return json(['status' => 'success', 'message' => '取消成功']);
        }
    }
}